package com.gitee.jjnnzb.designpatterninspring.common.config;

import cn.hutool.core.io.resource.ClassPathResource;
import com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.entity.dto.Trie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;

import java.io.InputStream;

/**
 * @author Jjn
 * @since 2023/8/11 14:35
 */
@Configuration
@Slf4j
public class AcTrieConfig {
    @Bean(name = "idiomTrie")
    public Trie initIdiomTrie() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        if (log.isInfoEnabled()) {
            log.info("加载成语词库");
        }
        InputStream inputStream = new ClassPathResource("dict/ChengYu_Corpus（5W）.txt").getStream();
        if (log.isInfoEnabled()) {
            log.info("构建成语Trie");
        }
        Trie trie = new Trie();
        trie.addKeywords(inputStream);
        stopWatch.stop();
        if (log.isInfoEnabled()) {
            log.info("构建成语Trie完毕,累计耗时:{}s", stopWatch.getTotalTimeSeconds());
        }
        return trie;
    }
    
    @Bean(name = "regionTrie")
    public Trie initCRegionTrie() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        if (log.isInfoEnabled()) {
            log.info("加载地域词库");
        }
        Trie trie = new Trie();
        if (log.isInfoEnabled()) {
            log.info("构建地域Trie");
        }
        InputStream inputStream = new ClassPathResource("dict/THUOCL_diming.txt").getStream();
        if (log.isInfoEnabled()) {
            log.info("构建成语Trie");
        }
        trie.addKeywords(inputStream);
        stopWatch.stop();
        if (log.isInfoEnabled()) {
            log.info("构建地域Trie完毕,累计耗时:{}s", stopWatch.getTotalTimeSeconds());
        }
        return trie;
    }
}
